package encrypt;


import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 * 加解密
 * 此类自己生成1对公私钥
 * 模拟 私钥加密公钥验签
 */
public class SHA1withRSAUtils {


    private String privatekey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALAuClXrp1xYGVIbpGYcioy33A+0C2oNudyTM3ihvptVLz/h+b4QM+Fw3Qj/jiDUBvckgf4UopSPIL1qsbxbed0HmQ3CCjrdQMov1bBwZDj1kW6v9b+1VEKTXvcdc68zKyi5ecEKgfgFDogbEFG4v/N0ig7nV8zpd8S2h/IvzHytAgMBAAECgYBWGLnOUVJBa4A8QuvtRsdiiztMpUTqt/h7VahSrGB0koV7p5gfjNV9t2UdT54KZvNcdYfdPgJRrTxntKtVDDMZwQAVKcFev2FMqzhPad9pL+jFzwfRKilGQnL8qHUCaTi6s7cUDutjtkA1yekbVnofAdoWj7xEEx9CCWXtQGloIQJBAP4dLWiyzNSQP7fi9C5kH8/Hs6KIUOvV8nkI3NJw5XgE1Y9Vn1MD/c69jgnAyOEnkV9iYJee35xho91pzJ+aYrkCQQCxfMlKLI7XG2kLanovITSwl1n3yu+xyjankTQ4NqIhcDL670pEPePyImx9d9/gRbJwZbKAVKtIHrY4iJZ1Eb+VAkAcBaWrRQRXcGzGX22j7ACiDhieFMAvsZEijbqkLjTvzkCqpLuEEQ2dDcQrCSpdwdpDFXsnXSq1eQLz+KXK9+BpAkBT21VvSiYi07yJvEEGS3YNiTTksmkVYyj3es9lT8rCabRsgupf2637FPCVRzHrUILCQApaP6a9aGVHyKDtpvkRAkEA/gbBLY6TqaLB6k9BH/wJRj6ke/JaDtM9KuMminJVyNlS2+xio6x0BSuD4HqWdDY8gQOq4mnnKRa4505kCtDk4Q==";
    private String publickey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwLgpV66dcWBlSG6RmHIqMt9wPtAtqDbnckzN4ob6bVS8/4fm+EDPhcN0I/44g1Ab3JIH+FKKUjyC9arG8W3ndB5kNwgo63UDKL9WwcGQ49ZFur/W/tVRCk173HXOvMysouXnBCoH4BQ6IGxBRuL/zdIoO51fM6XfEtofyL8x8rQIDAQAB";


    /**
     * 私钥加密
     */
    public String signData(String reqJsonStr) throws Exception {
        byte[] reqJsonBytes = reqJsonStr.getBytes();

        byte[] privateKeyBytes = Base64.decode(privatekey);

        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));


        Signature sign = Signature.getInstance("SHA1withRSA");//SHA1withRSA算法进行签名
        sign.initSign(privateKey);//初始化私钥签名
        sign.update(reqJsonBytes);//更新用于签名的数据
        byte[] signature = sign.sign();//生成签名
        String encode = Base64.encode(signature);//base64编码
        return encode;
    }


    /**
     * 公钥验签名
     */
    public boolean signDataVerify(String soureData, String signData) throws Exception {
        KeyFactory kf = KeyFactory.getInstance("RSA");

        //公钥还原
        byte[] pubKeyBytes = Base64.decode(publickey);//base解码
        PublicKey publicKey = kf.generatePublic(new X509EncodedKeySpec(pubKeyBytes));


        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initVerify(publicKey);
        signature.update(soureData.getBytes());

        // 验证签名
        byte[] signDataBytes = Base64.decode(signData);//base解码
        Boolean verifyResult = signature.verify(signDataBytes);
        return verifyResult;
    }


    public static void main(String[] args) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("1", 1);
        map.put("2", 2);

        String sourceStr = JSON.toJSONString(map);

        SHA1withRSAUtils apiUtil = new SHA1withRSAUtils();
        String signData = apiUtil.signData(sourceStr);//获取签名
        System.out.println("签名加密结果:" + signData);
        System.out.println("签名验签结果:" + apiUtil.signDataVerify(sourceStr, signData));
    }
}
